import { request } from '@/service/request'
import { CONTENT_TYPE } from '@/constants/service'
import { localStg } from '@/utils'
// import type { User } from './types'

/**
 * 根据模板名称查询合同模板
 * @param hospital 医院对象，需包含 operatorid、proxyorganizationopenid 等字段
 * @param templateName 合同模板名称
 * @returns Promise<模板对象|null>
 */
export async function getContractTemplateByName(hospital: any, templateName: string) {
  if (!hospital?.operatorid) {
    throw new Error('医院信息不完整，缺少 operatorid')
  }
  const { success } = await request.post(
    '/tencent_esign/tencent_contract/tencentContractInfo/getDescribeTemplates',
    {
      operatorId: hospital.operatorid,
      proxyOrganizationOpenId: hospital.proxyorganizationopenid,
      // proxyOrganizationName: hospital.proxyorganizationname,
    },
    {
      contentType: CONTENT_TYPE.formUrlencoded,
      useErrMsg: true
    }
  )
  if (Array.isArray(success) && success.length > 0) {
    return success.find((item: any) => item.templateName === templateName) || null
  }
  return null
}

/**
 * 创建合同
 * @param hospital 医院对象
 * @param user 用户对象（需包含 idcard、name、phone 等）
 * @param templateId 合同模板ID
 * @param flowName 合同流程名称
 * @param flowDescription 合同流程描述
 * @returns Promise<any>
 */
export async function createContract({
  hospital,
  user,
  templateId,
  flowName,
  flowDescription
}: {
  hospital: any,
  user: any,
  templateId: string,
  flowName: string,
  flowDescription: string
}) {
  const data = {
    flowName,
    flowDescription,
    templateId,
    operatorId: hospital.operatorid,
    operatorName: hospital.operatorname,
    operatorTel: hospital.operatortel,
    proxyOrganizationName: hospital.proxyorganizationname,
    proxyOrganizationOpenId: hospital.proxyorganizationopenid,
    "signers[0].approverType": "PERSON",
    "signers[0].idCardNumber": user.idcard,
    "signers[0].idCardType": "ID_CARD",
    "signers[0].mobile": user.phone,
    "signers[0].name": user.name,
    "signers[0].signOrder": 1,
    // 可扩展更多签署人、formFields 等
  }
  return request.post(
    '/tencent_esign/tencent_contract/tencentContractInfo/createFlowByTemplate',
    data,
    {
      contentType: CONTENT_TYPE.formUrlencoded,
      useErrMsg: true
    }
  )
}

/**
 * 获取健康码二维码内容
 * @param user 用户对象（需包含 vuid）
 * @returns Promise<string | null> 健康码二维码内容字符串
 */
export async function getHealthCode(user: { vuid: string }) {
  const { success } = await request.post('/health_code/healthCode/getApply', {
    vuid: user.vuid,
  }, {
    useErrMsg: true
  })
  if (success?.bizContent?.ecContent) {
    return success.bizContent.ecContent
  }
  return null
}

/**
 * 注册健康码，返回二维码内容
 * @param user 用户对象（需包含 name、idCard、openid）
 * @returns Promise<string | null> 健康码二维码内容字符串
 */
export async function registerHealthCode(user: { name: string, idcard: string, openid: string }) {
  // const name = localStg.get("name");
  // const openid = localStg.get("openid");
  // const idcard = localStg.get("idcard");
  const { success } = await request.post('/health_code/healthCode/getRegquery', {
    appMode: "1",
    klx: "1",
    idType: "01",
    name: user.name,
    idNo: user.idcard,
    openid: user.openid
  }, {
    useErrMsg: true
  })
  if (success?.bizContent?.ecContent) {
    return success.bizContent.ecContent
  }
  return null
}

/**
 * 将 User 类型对象转换为 registerHealthCode 所需参数
 */
// export function userToRegisterHealthCodeInput(user: User, openid: string) {
//   return {
//     name: user.name,
//     idcard: user.idcard,
//     phone: user.phone,
//     openid
//   }
// } 